package com.dudy;

import java.util.Observable;
import java.util.Observer;

/**
 * @author <a href="mailto:zjw2023@163.com">zjw2023</a>
 * @version 1.0
 * @Description
 * @Since 2016/5/23
 */
public class Observer2 implements Observer {
    private Observable observable;
    private String name;
    private double price;

    /**
     * 构造方法
     * 定义其使用的可观察者
     * 并将当前观察者添加到可观察者中
     * @param observable
     */
    public Observer2(Observable observable){
        this.observable = observable;
        observable.addObserver(this);
    }

    /**
     * 实现update方法
     * @param o 可观察者对象
     * @param arg 变更的参数  由Observers中notifyObservers(Observer o,Object arg)传递过来的变更的对象
     */
    @Override
    public void update(Observable o, Object arg) {
        if(o instanceof SubjectObservable){
            SubjectObservable subjectObservable = (SubjectObservable)o;
            /**
             * 观察者自己定义需要获取的数据
             */
            this.name = subjectObservable.getName();
            this.price = subjectObservable.getPrice();
            System.out.println("name has changed to "+name+",price has changed to "+price);
        }
    }
}
